package com.aaa.controller;

import com.aaa.entity.R;
import com.aaa.service.VodService;
import com.aaa.utils.AliyunVideoPath;
import com.aaa.utils.ConstantVodUtils;
import com.aaa.utils.InitVodCilent;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: VodController
 * @Description: TODO
 * @Author: 86139
 * @Date: 2021/9/14 21:10
 * @Version:1.0
 **/

@RestController
@RequestMapping("/video")
@CrossOrigin
public class VodController {
    @Resource
    VodService vodService;

    //上传视频到阿里云
    @PostMapping("uploadAlyiVideo")
    public R uploadAlyiVideo(@RequestParam("head") MultipartFile file) {
        //返回上传视频id
        String videoId = vodService.uploadVideoAly(file);
        System.out.println(videoId);
        return R.ok().data("videoId",videoId);
    }
    @RequestMapping("url")
    public String videoURL(String videoId){
        System.out.println(videoId);
        try {
            String playUrL = AliyunVideoPath.getPlayUrL(videoId);
            System.out.println(playUrL);
            return playUrL;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }
    //根据视频id删除阿里云视频
    @DeleteMapping("removeAlyVideo/{id}")
    public R removeAlyVideo(@PathVariable String id) {
        try {
            //初始化对象
            DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置视频id
            request.setVideoIds(id);
            //调用初始化对象的方法实现删除
            client.getAcsResponse(request);
            return R.ok();
        }catch(Exception e) {
            e.printStackTrace();
            return  R.error().data("20001","删除视频失败");
        }
    }

    //删除多个阿里云视频的方法
    //参数多个视频id  List videoIdList
    @DeleteMapping("delete-batch")
    public R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList) {
        vodService.removeMoreAlyVideo(videoIdList);
        return R.ok();
    }

}
